package loupan_utils

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/e"
	"hft/tools/phone/api"
	"hft/tools/phone/model"
	"regexp"
	"strings"

	"gorm.io/gorm"
)

// 判断删除的轮播图是否属于该区域
func IsTheseSwiperBelongThisLou(list []*admin.LouPanSwiper, louID uint) bool {
	for _, v := range list {
		if v.LouPanID != int(louID) {
			return false
		}
	}
	return true
}

// 远端添加400电话
func Add400PhoneRemote(db *gorm.DB, dn int, louPanName string, Tel string) (err error) {
	var phoneItem []*model.Row
	phoneItem = append(phoneItem, &model.Row{Dn: fmt.Sprintf("%v", dn), Tel: Tel, TransType: "rnd"})
	code := api.PhoneVirtual.AddOrEditPhone(phoneItem)
	fmt.Println("代码", code, "hhhhh")
	if code != "0" {
		return errors.New(e.Phone400AddFail)
	}
	return nil
}

// 上传语音
func AddAudio(dn string, name string, db *gorm.DB) (err error) {
	// 创建语音
	var audioList = make([]*model.Audio, 0)
	var comeName = fmt.Sprintf("%v_ani.wav", dn)
	var inName = fmt.Sprintf("%v_dni.wav", dn)
	var come = &model.Audio{Name: comeName, Content: fmt.Sprintf("欢迎致电%v线上售楼处", name)}
	var in = &model.Audio{Name: inName, Content: fmt.Sprintf("您有新的客户,在%v楼盘来电", name)}
	audioList = append(audioList, come, in)
	code := api.PhoneAudio.UploadAudio(audioList)
	if code != "0" {
		return errors.New(e.Phone400AudioUploadFail)
	}
	return nil
}

// 将删除失败的语音丢入mongo中用定时任务执行删除
func DelayDeleteAudio(audioNames []*model.AudioName) {

}

// 删除楼盘对应的经纪人手机
func DeleteBrokerPhoneBelongThisLouPan(phone string, managerPhone string, tel string) string {
	var telArr = strings.Split(tel, ",")
	telArr1 := telArr[2 : len(telArr)-1]
	fmt.Println(phone, managerPhone, telArr1)
	if len(telArr1) == 0 {
		// 如果只剩一个手机号 换成管理员的手机
		return fmt.Sprintf("%v", managerPhone)
	} else {
		// 删除对应的手机号
		newTel := ProcessSlice(telArr1, phone, managerPhone)
		return fmt.Sprintf("%v", newTel)
	}
}

// 获取新的	Tel
func ProcessSlice(a []string, b, c string) string {
	newSlice := make([]string, 0)
	for _, item := range a {
		if item != b {
			newSlice = append(newSlice, item)
		}
	}

	if len(newSlice) == 0 {
		return c
	}

	joined := strings.Join(newSlice, ",")
	return joined
}

// 添加楼盘时获取新的Tel
func GetNewTelWhenAddNewLouPan(phone string, managerPhone string, tel string) string {
	var telArr = strings.Split(tel, ",")
	telArr1 := telArr[1 : len(telArr)-1]
	// 如果只有一个手机号且该手机号为管理员手机号则替换
	if len(telArr1) == 1 && telArr1[0] == managerPhone {
		return phone
	} else {
		// 如果不是就直接在后面加入
		telArr1 = append(telArr1, phone)
		return strings.Join(telArr1, ",")
	}
}

// 判断是否有权修改经纪人们的信息
func HasPowerToOperaBrokers(accountLink string, broker []*admin.Account) bool {
	for _, v := range broker {
		if ok, _ := regexp.MatchString(fmt.Sprintf("^%v", accountLink), fmt.Sprintf("%v%v,", v.AccountLink, v.ID)); !ok {
			return false
		}
	}
	return true
}

// 批量添加经纪人手机
func BatchAddBrokerPhoneToThisLouPan(broker []*admin.Account, managerPhone string, tel string) string {
	if len(broker) == 0 {
		return tel
	}
	var telArr = strings.Split(tel, ",")
	telArr1 := telArr[1 : len(telArr)-1]
	var newPhoneStr []string
	for _, v := range broker {
		newPhoneStr = append(newPhoneStr, v.Phone)
	}
	if telArr1[0] == managerPhone {
		return strings.Join(newPhoneStr, ",")
	} else {
		return strings.Join(append(telArr1, newPhoneStr...), ",")
	}
}
